<?php


namespace app\app\controller;


use think\Db;

class Pz extends WxUser
{
    protected $PzInfo;
    protected $PzId;
    protected $NurseInfo;
    protected $NurseId;
    protected $Role; //1 陪诊员 2护士 3护士陪诊员

    protected function initialize()
    {
        parent::initialize();
        $pz_info = Db::name('pz')->where([
            'member_id' => $this->memberId,
            'is_del' => 0,
            'type' => 1
        ])->find();
        $nurse_info = Db::name('pz')->where([
            'member_id' => $this->memberId,
            'is_del' => 0,
            'type' => 2
        ])->find();
        if ((!$pz_info) && (!$nurse_info)) {
            responseJson(-100, '服务端信息不存在');
        }
        if (($pz_info['status'] != 1) && ($nurse_info['status'] != 1)) {
            responseJson(-200, '状态异常，请联系管理员');
        }
        if ($pz_info) {
            $this->PzInfo = $pz_info;
            $this->PzId = $pz_info['id'];
            $this->PzInfo['count'] = $this->getCount($this->PzInfo['pz_level_id']);
        }
        if ($nurse_info) {
            $this->NurseInfo = $nurse_info;
            $this->NurseId = $nurse_info['id'];
            $this->NurseInfo['count'] = $this->getCount($this->NurseInfo['pz_level_id']);
        }
        if ($pz_info && $nurse_info) {
            $this->Role = 3;
        } else if ($pz_info) {
            $this->Role = 1;
        } else if ($nurse_info) {
            $this->Role = 2;
        }
    }

    public function getCount($pz_level_id)
    {
        $pz_level_info = Db::name('pz_level')->find($pz_level_id);
        return number_format((100 - $pz_level_info['count']) / 100, 2);
    }

    //陪诊员 0下线 1上线
    public function BusinessOnline()
    {
        $data = $this->request->param();
//        $update = array(
//            'id' => $this->PzId,
//            'is_online' => $data['is_online']
//        );
        $update = array(
            'is_online' => $data['is_online']
        );
        Db::name('pz')->where('member_id', '=', $this->memberId)->update($update);
        msg(0, '更改成功', $data['is_online']);
    }

    //获取陪诊员信息
    public function getPzInfo()
    {
        $data = $this->request->param();
        if ($data['type'] == 1) {
            if ($this->PzInfo) {
                $this->PzInfo['role'] = $this->Role;
                msg(0, '查询成功', $this->PzInfo);
            }
        }
        if ($data['type'] == 2) {
            if ($this->NurseInfo) {
                $this->NurseInfo['role'] = $this->Role;
                msg(0, '查询成功', $this->NurseInfo);
            }
        }
        msg(-1, '信息不存在', '');
    }

    //获取护士信息 todo
    public function getNurseInfo()
    {
        if ($this->NurseInfo) {
            msg(0, '查询成功', $this->NurseInfo);
        }
        msg(-1, '护士信息不存在', '');
    }
    //1待支付2带指派3服务中4已完成5已取消6已退款7已指派8已评价
    //接单大厅列表
    public function roomOrderList()
    {
        $is_online = $this->serverOnlineStatus();
        if (!$is_online) {
            msg(-2, '未上线', '');
        }
        //'订单状态 1待支付2带指派3服务中4已完成5已取消6已退款7已指派8已评价'
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        if ($this->Role == 1) {
            $where[] = ['order_type', '=', 1];
        } else if ($this->Role == 2) {
            $where[] = ['order_type', 'in', [1, 2]];
        } else if ($this->Role == 3) {
            $where[] = ['order_type', 'in', [1, 2]];
        }
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', '=', 2];
        $where[] = ['dispatch_able', '=', 1];
        $data_list = Db::name('server_order')
            ->where($where)
            ->order('pay_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['time'] = date('Y-m-d H:i:s', $v['time']);
            $server_info = Db::name('server')->find($v['server_id']);
            $data_list[$k]['server_info'] = $server_info;
            $data_list[$k]['get_money'] = $this->getMoney($v);
        }
        msg(0, '查询成功', $data_list);
    }

    //陪诊接单
    public function ReceivingServerOrder()
    {
        $data = $this->request->param();
        $order_info = Db::name('server_order')->find($data['id']);
        if (!$order_info) {
            msg(-1, '订单不存在', '');
        }
        if ($order_info['status'] == 7) {
            msg(-2, '订单已经分配', 1);
        }
        $update = array(
            'id' => $data['id'],
            'status' => 7,
            'dispatch_type' => 1,
            'dispatch_time' => time(),
        );
        if ($order_info['order_type'] == 1) {
            if ($this->Role == 1) {
                $update['pz_id'] = $this->PzId;
            } else if ($this->Role == 2) {
                $update['pz_id'] = $this->NurseId;
            } else if ($this->Role == 3) {
                $update['pz_id'] = $this->NurseId;
            }
        } else if ($order_info['order_type'] == 2) {
            $update['pz_id'] = $this->NurseId;
        }
        Db::startTrans();
        try {
            $update_data = Db::name('server_order')->update($update);
            Db::commit();
            msg(0, '查询完成', $update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //待处理订单列表
    public function waitOrderList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', '=', 7];
        $where[] = ['pz_id', 'in', [$this->NurseId, $this->PzId]];
        $data_list = Db::name('server_order')
            ->where($where)
            ->order('dispatch_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['time'] = date('Y-m-d H:i:s', $v['time']);
            $server_info = Db::name('server')->find($v['server_id']);
            $data_list[$k]['server_info'] = $server_info;
            $data_list[$k]['get_money'] = $this->getMoney($v);
            $data_list[$k]['yyzzImgs']='';
        }
        msg(0, '查询成功', $data_list);
    }

    //陪诊员开始处理订单
    public function handleOrder()
    {
        $data = $this->request->param();
        $update = array(
            'id' => $data['id'],
            'status' => 3,
            'start_time' => time(),
        );
        $order_info = Db::name('server_order')->find($data['id']);
        if (!$order_info) {
            msg(-1, '订单不存在', '');
        }
        $update['get_money'] = $this->getMoney($order_info);
        Db::startTrans();
        try {
            $update_data = Db::name('server_order')->update($update);
            if (isset($data['img']) && $data['img']) {
                $server_order_img_insert = array(
                    'order_id' => $order_info['id'],
                    'order_no' => $order_info['order_no'],
                    'type' => 1,
                    'create_time' => time(),
                    'img_url' => $data['img']
                );
                Db::name('server_order_img')->where(['order_id' => $order_info['id'], 'type' => 1])->delete();
                Db::name('server_order_img')->insertGetId($server_order_img_insert);
            }
            Db::commit();
            msg(0, '查询完成', $update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //陪诊员是否点击完成
    public function pzEndOrder()
    {
        $data = $this->request->param();
        $update = array(
            'id' => $data['id'],
            'pz_is_end' => 1,
            'pz_end_time' => time(),
        );
        $order_info = Db::name('server_order')->find($data['id']);
        if (!$order_info) {
            msg(-1, '订单不存在', '');
        }
        Db::startTrans();
        try {
            $update_data = Db::name('server_order')->update($update);
            if (isset($data['img']) && $data['img']) {
                $server_order_img_insert = array(
                    'order_id' => $order_info['id'],
                    'order_no' => $order_info['order_no'],
                    'type' => 2,
                    'create_time' => time(),
                    'img_url' => $data['img']
                );
                Db::name('server_order_img')->where([
                    'order_id' => $order_info['id'],
                    'type' => 2,
                ])->delete();
                Db::name('server_order_img')->insertGetId($server_order_img_insert);
            }
            Db::commit();
            msg(0, '查询完成', $update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    public function pzEndOrder2()
    {
        $data = $this->request->param();
        $update = array(
            'id' => $data['id'],
            'pz_is_end' => 1,
            'pz_end_time' => time(),
        );
        $order_info = Db::name('server_order')->find($data['id']);
        if (isset($data['pz_comment']) && $data['pz_comment']) {
            Db::name('server_order')->update([
                'id' => $data['id'],
                'pz_comment' => $data['pz_comment']
            ]);
        }
        if (!$order_info) {
            msg(-1, '订单不存在', '');
        }
        Db::startTrans();
        try {
            $update_data = Db::name('server_order')->update($update);
            if (isset($data['img']) && $data['img']) {
                $server_order_img_insert = array(
                    'order_id' => $order_info['id'],
                    'order_no' => $order_info['order_no'],
                    'type' => 3,
                    'create_time' => time(),
                    'img_url' => $data['img']
                );
                Db::name('server_order_img')->where([
                    'order_id' => $order_info['id'],
                    'type' => 3,
                ])->delete();
                Db::name('server_order_img')->insertGetId($server_order_img_insert);
            }
            Db::commit();
            msg(0, '查询完成', $update_data);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //服务中订单列表
    public function getServerOrderList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', 'in', [3]];
        $where[] = ['pz_id', 'in', [$this->NurseId, $this->PzId]];
        $data_list = Db::name('server_order')
            ->where($where)
            ->order('start_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['fwImgs']='';
            $data_list[$k]['time'] = date('Y-m-d H:i:s', $v['time']);
            $data_list[$k]['dispatch_time'] = date('Y-m-d H:i:s', $v['dispatch_time']);
            $data_list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
            $server_info = Db::name('server')->find($v['server_id']);
            $data_list[$k]['server_info'] = $server_info;

            $server_order_img = Db::name('server_order_img')->where([
                'order_id' => $v['id']
            ])->order('type')->select();
            foreach ($server_order_img as $k2 => $v2) {
                $server_order_img[$k2]['create_time'] = date('Y-m-d H:i:s', $server_order_img[$k2]['create_time']);
                switch ($v2['type']) {
                    case 1:
                        $server_order_img[$k2]['desc'] = '开始服务';
                        break;
                    case 2:
                        $server_order_img[$k2]['desc'] = '服务中';
                        break;
                    case 3:
                        $server_order_img[$k2]['desc'] = '服务结束';
                        break;
                    default:
                        $server_order_img[$k2]['desc'] = '未知';
                }
            }
            $data_list[$k]['server_order_img'] = $server_order_img;
        }
        msg(0, '查询成功', $data_list);
    }


    //陪诊历史订单
    public function historyServerOrder()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['is_del', '=', 0];
        $where[] = ['status', 'in', [4, 8]];
        $where[] = ['pz_id', 'in', [$this->NurseId, $this->PzId]];
        $data_list = Db::name('server_order')
            ->where($where)
            ->order('end_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['WcfwImgs']='';
            $data_list[$k]['time'] = date('Y-m-d H:i:s', $v['time']);
            $data_list[$k]['dispatch_time'] = date('Y-m-d H:i:s', $v['dispatch_time']);
            $data_list[$k]['start_time'] = date('Y-m-d H:i:s', $v['start_time']);
            $data_list[$k]['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
            $hospital_info = Db::name('hospital')->find($v['hospital_id']);
            $data_list[$k]['hospital_name'] = $hospital_info['name'];
            $server_info = Db::name('server')->find($v['server_id']);
            $data_list[$k]['server_info'] = $server_info;
            $server_order_img = Db::name('server_order_img')->where([
                'order_id' => $v['id']
            ])->order('type')->select();
            foreach ($server_order_img as $k2 => $v2) {
                $server_order_img[$k2]['create_time'] = date('Y-m-d H:i:s', $server_order_img[$k2]['create_time']);
                switch ($v2['type']) {
                    case 1:
                        $server_order_img[$k2]['desc'] = '开始服务';
                        break;
                    case 2:
                        $server_order_img[$k2]['desc'] = '服务中';
                        break;
                    case 3:
                        $server_order_img[$k2]['desc'] = '服务结束';
                        break;
                    default:
                        $server_order_img[$k2]['desc'] = '未知';
                }
            }
            $data_list[$k]['server_order_img'] = $server_order_img;
        }
        msg(0, '查询成功', $data_list);
    }


    //账户中心顶部
    public function pzFinanceTop()
    {
        //可提现
        //待入账
        if ($this->Role == 1) {
            $where_dai = [
                ['status', 'in', [3]],
                ['is_del', '=', 0],
                ['pz_id', '=', $this->PzId]
            ];
        } else if ($this->Role == 2) {
            $where_dai = [
                ['status', 'in', [3]],
                ['is_del', '=', 0],
                ['pz_id', '=', $this->NurseId]
            ];
        } else {
            $where_dai = [
                ['status', 'in', [3]],
                ['is_del', '=', 0],
                ['pz_id', 'in', [$this->NurseId, $this->PzId]]
            ];
        }

        $detail_money = Db::name('server_order')
            ->where($where_dai)
            ->sum('get_money');
        $b = $detail_money;
        //提现中
        $c = Db::name('member_finance')->where([
            ['type', '=', 3],
            ['is_kuan', '=', 0],
            ['member_id', '=', $this->memberId]
        ])->sum('money');
        //已提现
        $d = Db::name('member_finance')->where([
            ['type', '=', 3],
            ['is_kuan', '=', 1],
            ['member_id', '=', $this->memberId]
        ])->sum('money');
        $return['a'] = $this->memberInfo['account'];
        $return['b'] = $b;
        $return['c'] = $c;
        $return['d'] = $d;
        msg(0, '查询成功', $return);
    }

    //收支明细
    public function szList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['member_id', '=', $this->memberId];
        //1收入 2支出
        if ($data['type'] == 1) {
            $where[] = ['type', 'in', [1]];
        } elseif ($data['type'] == 2) {
            $where[] = ['type', 'in', [3]];
        } else {
            $where[] = ['type', 'in', [1, 3]];
        }
        if (isset($data['time']) && $data['time']) {
            $startOfDay = strtotime($data['time']);
            $endOfDay = strtotime($data['time'] . '23:59:59');
            $where[] = ['create_time', 'between', [$startOfDay, $endOfDay]];
        }
        $data_list = Db::name('member_finance')
            ->where($where)
            ->order('create_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        msg(0, '查询成功', $data_list);
    }

    /**
     * 陪诊员提现
     */
    public function pzTixian()
    {
        $data = $this->request->param();
        if ($data['amount'] <= 0) {
            msg(-1, '输入正确值', '');
        }

        if ($data['amount'] > $this->memberInfo['account']) {
            msg(-1, '提现金额不能大于余额', '');
        }
        Db::startTrans();
        try {
            $insert_data = array(
                'member_id' => $this->memberId,
                'money' => $data['amount'],
                'type' => 3,
                'tixian_type' => $data['tixian_type'], //1银行 2支付宝 3微信
                'tixian_account' => $data['tixian_account'], //提现账号
                'tixian_name' => $data['tixian_name'],//提现人姓名
                'tixian_bank' => $data['tixian_bank'],//提现银行名（银行提现使用）
                'desc' => '余额提现',
                'status' => 0,//'0未完成 待审核 1已完成 审核完成 提现使用',
                'create_time' => time(),
                'is_count' => 2
            );
            $insert_id = Db::name('member_finance')->insertGetId($insert_data);
            Db::name('member')
                ->where('id', '=', $this->memberId)
                ->setDec('account', $data['amount']);
            Db::commit();
            msg(0, '更新成功', $insert_id);
        } catch (\Exception $e) {
            Db::rollback();
            msg(-1, $e->getMessage(), '');
        }
    }

    //提现记录
    public function tixianList()
    {
        $data = $this->request->param();
        $page = input('page/d', 1);
        $length = input('limit/d', 20);
        $where[] = ['member_id', '=', $this->memberId];
        $where[] = ['type', '=', 3];
        $data_list = Db::name('member_finance')
            ->where($where)
            ->order('create_time desc')
            ->page($page, $length)
            ->select();
        foreach ($data_list as $k => $v) {
            $data_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        msg(0, '查询成功', $data_list);
    }

    //服务人员上下线状态
    public function serverOnlineStatus()
    {
        switch ($this->Role) {
            case 1:
                return $this->PzInfo['is_online'];
            case 2:
                return $this->NurseInfo['is_online'];
            case 3:
                if ($this->PzInfo['is_online'] && $this->NurseInfo['is_online']) {
                    return 1;
                } else {
                    return 0;
                }
        }
    }

    public function onlineStatus()
    {
        msg(0, '查询成功', $this->serverOnlineStatus());
    }

    public function getMoney($order_info)
    {
        $get_money = -1;
        if ($order_info['order_type'] == 1) {
            //1陪诊员 2护士 3护士陪诊员
            if ($this->Role == 1 || $this->Role == 3) {
                $pz_info = Db::name('pz')->where(['is_del' => 0, 'type' => 1, 'member_id' => $this->memberId])->find();
                $pz_level_money_info = Db::name('pz_level_money')
                    ->where([
                        'pz_level_id' => $pz_info['pz_level_id'],
                        'server_id' => $order_info['server_id']
                    ])->find();
                $get_money = number_format($pz_level_money_info['money'], 2);
            } else if ($this->Role == 2) {
                $nurse_info = Db::name('pz')->where(['is_del' => 0, 'type' => 2, 'member_id' => $this->memberId])->find();
                $pz_level_money_info = Db::name('pz_level_money')
                    ->where([
                        'pz_level_id' => $nurse_info['pz_level_id'],
                        'server_id' => $order_info['server_id']
                    ])->find();
                $get_money = number_format($pz_level_money_info['money'], 2);
            }
        } else if ($order_info['order_type'] == 2) {
            $nurse_info = Db::name('pz')->where(['is_del' => 0, 'type' => 2, 'member_id' => $this->memberId])->find();
            $pz_level_money_info = Db::name('pz_level_money')
                ->where([
                    'pz_level_id' => $nurse_info['pz_level_id'],
                    'server_id' => $order_info['server_id']
                ])->find();
            $get_money = number_format($pz_level_money_info['money'], 2);
        }
        if ($order_info['is_new_card'] == 1) {
            $get_money = config('site.new_card_moeny') ? config('site.new_card_moeny') : 90;
        }
        return $get_money;
    }

}